package com.example.licenseappclient;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.stage.Stage;

import java.io.IOException;

public class PurchaseController {
    String licenseType="One";

    @FXML
    private Button OkButton;

    @FXML
    private MenuButton ShowTypeButton;

    @FXML
    private TextField UserNameInput;

    @FXML
    private Button CancelButton;

    @FXML
    private TextField CDKInput;

    @FXML
    private MenuItem LicenseTypeOne;

    @FXML
    private MenuItem LicenceTypeTwo;

    @FXML
    void ChangeToOne(ActionEvent event) {
        licenseType="One";
        ShowTypeButton.setText("1人");
    }

    @FXML
    void ChangeToTwo(ActionEvent event) {
        licenseType="Two";
        ShowTypeButton.setText("2人");
    }

    @FXML
    void Cancel(ActionEvent event) {
        Stage stage =(Stage) ((Node)event.getSource()).getScene().getWindow();
        stage.close();
    }

    @FXML
    void GetLoginCode(ActionEvent event) {
        String regiMsg= getRegiMsg();
        if(regiMsg==null){
            showAlert(Alert.AlertType.WARNING,"输入错误","输入错误","请检查输入正确后再次购买");
        }
        else GoAndRigi(regiMsg);
    }

    public void GoAndRigi(String msg){
        try {
            SocketHelper socketHelper = new SocketHelper("127.0.0.1",8090);
            socketHelper.sendMessage(msg);

//            socketHelper.sendMessage("测试，客户端访问");
            String message;
            if(!(message= socketHelper.receiveMessage()).isEmpty()) {
                System.out.println(message);
                checkReturnMessage(message);
            }


            System.out.println("Debug d");

            if(socketHelper!=null){
                socketHelper.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void initialize(){
        licenseType="One";
        ShowTypeButton.setText("1人");
    }

    private String getRegiMsg(){
        StringBuilder sb = new StringBuilder("Regi:");
        String read;
        read = UserNameInput.getText();
        if(read.isEmpty()) return null;
        sb.append(read);
        sb.append(",");
        read= CDKInput.getText();
        if(read.isEmpty()) return null;
        sb.append(read);
        sb.append(",");
        sb.append(licenseType);
        return sb.toString();
    }

    private void showAlert(Alert.AlertType type, String title, String header, String content) {
        Alert alert = new Alert(type);
        alert.setTitle(title);
        alert.setHeaderText(header);
        alert.setContentText(content);
        alert.showAndWait();
    }

    public void checkReturnMessage(String msg){
        String [] rmsg= msg.split(":");
        if(rmsg.length<2) return;
        if(rmsg[0].equals("Seri")){
            showAlert(Alert.AlertType.INFORMATION,"购买成功",rmsg[1],"恭喜你购买成功，你的序列号如上");
            closeAlert();
        }
        if(rmsg[0].equals("Regi")){
            showAlert(Alert.AlertType.INFORMATION,"购买失败","购买失败","抱歉，你的购买发生错误");
        }
    }

    private void closeAlert(){
        Stage stage =(Stage) ((Node) CancelButton).getScene().getWindow();
        stage.close();
    }

}
